package com.message.util;

import com.dingtalk.api.response.OapiV2UserListResponse;
import com.message.config.ConfigProperties;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;

import static com.message.consts.MessageConst.OPENPROJECT_DINGDING_YS;

public class  JedisPoolUtil {
    private volatile static JedisPool pool=null;

    //静态代码初始化池配置
    static {
        if (pool == null) {
            synchronized (JedisPoolUtil.class) {
                if (pool == null) {//双重校验锁，防止多线程环境下出现初始化多个线程池的情况
                    JedisPoolConfig config = new JedisPoolConfig();
                    //设置池配置项值
                    config.setMaxTotal(Integer.valueOf(ConfigProperties.getPropertyValue("jedis.pool.maxActive")));
                    config.setMaxIdle(Integer.valueOf(ConfigProperties.getPropertyValue("jedis.pool.maxIdle")));
                    config.setMaxWaitMillis(Long.valueOf(ConfigProperties.getPropertyValue("jedis.pool.maxWait")));
                    config.setTestOnBorrow(Boolean.valueOf(ConfigProperties.getPropertyValue("jedis.pool.testOnBorrow")));
                    config.setTestOnReturn(Boolean.valueOf(ConfigProperties.getPropertyValue("jedis.pool.testOnReturn")));
                    //根据配置实例化jedis池
                    if(ConfigProperties.getPropertyValue("redis.password")==null||"".equals(ConfigProperties.getPropertyValue("redis.password"))) {
                        pool = new JedisPool(config, ConfigProperties.getPropertyValue("redis.ip"), Integer.valueOf(ConfigProperties.getPropertyValue("redis.port")), Integer.parseInt(ConfigProperties.getPropertyValue("spring.redis.timeout")));
                    }else {
                        pool = new JedisPool(config, ConfigProperties.getPropertyValue("redis.ip"), Integer.valueOf(ConfigProperties.getPropertyValue("redis.port")), Integer.parseInt(ConfigProperties.getPropertyValue("spring.redis.timeout")), ConfigProperties.getPropertyValue("redis.password"));
                    }
                }
            }
        }
    }


    public static void initUserTable() {
        Jedis jedisObject = getJedisObject();
        jedisObject.del(OPENPROJECT_DINGDING_YS);
        try {
            //获取登录accessToken
            String accessToken = DingdingUtil.getAccessToken();
            List<Long> depList = DingdingUtil.getDepList(accessToken);
            for (int i = 0, len = depList.size(); i < len; i++) {
                List<OapiV2UserListResponse.ListUserResponse> depUserList = DingdingUtil.getDepuserIdList(depList.get(i), accessToken);
                for (int j = 0, len2 = depUserList.size(); j < len2; j++) {
                    jedisObject.hset(OPENPROJECT_DINGDING_YS, depUserList.get(j).getName(), depUserList.get(j).getUserid());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(jedisObject);
        }
    }

    /**
     * 获得jedis对象
     */
    public static Jedis getJedisObject() {
        return pool.getResource();
    }

    /**
     * 关闭jedis连接
     */
    public static void close(Jedis jedis) {
         jedis.close();
    }

}
